package helloworld;

import com.rabbitmq.client.*;
import org.junit.Test;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbitmq-demo1
 * @description: 消费者
 * @author: luolidong
 * @create: 2021-06-14 22:06
 * @version: 1.0
 */
public class Consumer {
    //    test会阻塞线程，输出不了数据
    //    @Test
//    public void getMsg() throws IOException, TimeoutException {
//
//    }
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.createConnection();
        Channel channel = connection.createChannel();
        /*
            绑定的通道要与发送的持久化配置一致
            自动删除，不是消费完就删除，而是与生产者断开连接才删除
        */
        channel.queueDeclare("queue3", true, false, true, null);
        channel.basicConsume("queue3", true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("msg:" + new String(body));
            }
        });
//        一般不关闭，一直监听
//        channel.close();
//        connection.close();
    }
}
